package dynamtic;

import java.util.Arrays;
import java.util.OptionalInt;
import java.util.Scanner;

/**
 * 最长递增子序列 or 最长递减子序列
 *
 * @Author Jun jie Yi
 * 2022/4/5 20:16
 */
public class LongArray {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        int[] arr1 = new int[num];
        for (int i = 0; i < arr1.length; i++) {
            arr1[i] = sc.nextInt();
        }
        int[] arr = Arrays.stream(arr1).distinct().toArray();
        int min = num;
        for (int i = 1; i < arr.length; i++) {
            //左边
            int[] l = new int[i];
            //初始化赋值
            for (int m = 0; m < i; m++) {
                l[m] = arr[m];
            }
            //右边
            int[] r = new int[arr.length - i];
            //初始化赋值
            for (int n = i; n < arr.length; n++) {
                r[n - i] = arr[n];
            }
            int re = arr1.length - (fun(l, 0) + fun(r, 1));
            if (min > re) {
                min = re;
            }
        }
        System.out.println(min);
    }

    //最长递增子序列 flag = 0，最长递减子序列 flag = 1
    public static int fun(int[] arr, int flag) {
        //定义一个dp数组
        int[] dp = new int[arr.length];
        Arrays.fill(dp, 1);//初始化值
        for (int i = 0; i < arr.length; i++) {
            //找到最大
            for (int j = 0; j < i; j++) {
                int max = Math.max(dp[j] + 1, dp[i]);
                if (flag == 0 && arr[i] > arr[j]) {// = 就不是递增了
                    dp[i] = max;
                } else if (flag == 1 && arr[i] < arr[j]) {
                    dp[i] = max;
                }
            }
        }
        OptionalInt max = Arrays.stream(dp).max();
        return max.getAsInt();
    }
}
